package com.example.qu_tao.View;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.qu_tao.R;
import com.example.qu_tao.adapter.GoodsListAdapter;
import com.example.qu_tao.contract.WodeHuopinContract;
import com.example.qu_tao.entity.Goods;
import com.example.qu_tao.presenter.WodeHuopinPresenter;

import java.util.ArrayList;
import java.util.List;

public class WodeHuopinActivity extends AppCompatActivity implements WodeHuopinContract.IWodeHuopinView {
    private RecyclerView recyclerView;
    private GoodsListAdapter adapter;
    private List<Goods> goodsList;
    private WodeHuopinContract.IWodeHuopinContract mWodeHuopinContract;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wode_huopin);

        // 初始化返回按钮
        initBackButton();

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.recyclerView);
        goodsList = new ArrayList<>();
        adapter = new GoodsListAdapter(this, goodsList);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);

        // 初始化Presenter
        mWodeHuopinContract = new WodeHuopinPresenter(this, this);
        mWodeHuopinContract.getGoodsList();
    }
    private void initBackButton() {
        ImageButton btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 结束当前Activity，返回卖家页面
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 处理物理返回键
        finish();
    }
    @Override
    public void showGoodsList(List<Goods> goodsList) {
        this.goodsList = goodsList;
        adapter.updateData(goodsList);
    }
}